import java.util.ArrayList;

import processing.core.*;


public class Loppu{
	PApplet pa;
	PImage taustakuva;
	PFont fontti;
	Hahmo mursu1, mursu2, mursu3;
	int voittoAika;
	Pokaali palkinto;


	Loppu(PApplet parent, ArrayList<Player> score, int voittajanAika){
		pa = parent;
		this.taustakuva = pa.loadImage("textures/Tausta5.png");
		this.fontti = pa.loadFont("fontti.vlw");

		this.mursuInit(score);
		this.voittoAika = voittajanAika;
		this.palkinto = new Pokaali(pa);
	}


	void piirra(){
		//		System.out.println("ASDFASDFASDFASDF");

		// spottivalot
		pa.noFill(); //XXX toimii ehka?
		pa.lights();
		pa.spotLight(200, 200, 200, 0, 300, 50, -1, 1, 0, 1, 2);
		pa.specular(204, 102, 0);
		pa.spotLight(250, 250, 250, 100, 100, 100, 0, 0, -1, 1, 2);

		this.kivaKamera();
		pa.background(86, 143, 211);
		pa.image(this.taustakuva, -15, -15);
		
		
		pa.pushMatrix();
		pa.translate(0, -20, 0);
		pa.pushMatrix();
		pa.translate(0, -15, 0);
		korokkeet();
		pa.popMatrix();
		teksti();
		this.mursuDraw();
		this.palkinto.piirra();
		pa.popMatrix();
	}

	private void kivaKamera() {
		float x = (float) (pa.width/2.0 + PApplet.cos((float) (pa.millis() / 1200.0)) * 22);
		float y = (float) (pa.height/2.0 + PApplet.sin((float) (pa.millis() / 1200.0)) * 22);
		pa.camera( x, y,(float) ((pa.height/2.0) / PApplet.tan((float) (PApplet.PI*60.0 / 360.0))), (float)(pa.width/2.0),(float) (pa.height/2.0),(float) 0,(float) 0,(float) 1,(float) 0);

	}

	private void mursuInit(ArrayList<Player> score) {		
		mursu1 = new Hahmo(pa, true, score.get(score.size()-1).playerNum);
		if (score.size()>1) {
			mursu2 = new Hahmo(pa, true, score.get(score.size()-2).playerNum);
			if (score.size() > 2) {
				mursu3 = new Hahmo(pa, true, score.get(score.size()-3).playerNum);
			}
		}
	}

	private void mursuDraw() {
		//2. mursu
		if (mursu2 != null) {
			pa.pushMatrix();
			pa.translate(300,560,30);					
			// scale((float)0.5);
			mursu2.mursu();
			pa.popMatrix();

		}
		// 1. mursu
		pa.pushMatrix();
		pa.translate(500,460,30);					
		// scale((float)0.5);
		mursu1.mursu();
		pa.popMatrix();

		// 3. mursu
		if (mursu3 != null) {
			pa.pushMatrix();
			pa.translate(700,560,30);					
			// scale((float)0.5);
			mursu3.mursu();
			pa.popMatrix();
		}
	}

	private void korokkeet(){
		pa.fill(224,238,224);
		pa.pushMatrix();
		pa.translate(500, 600, 40);
		pa.box(200);
		pa.popMatrix();

		pa.pushMatrix();
		pa.translate(300, 650, 40);
		pa.box(200,100,200);
		pa.popMatrix();

		pa.pushMatrix();
		pa.translate(700, 650, 40);
		pa.box(200,100,200);
		pa.popMatrix();
	}

	private void teksti(){

		pa.textFont(this.fontti);
		pa.fill(0);

		pa.text("Aikaa kului " + this.voittoAika +" s", 500,200,30);

		pa.textSize(18);
		pa.text("Paaset eteenpain klikkaamalla ruutua",500,250,30);

		pa.pushMatrix();
		//		pa.translate(0,-200,0);
		pa.textSize(60);
		pa.text("1", 485, 580, 145);

		pa.textSize(50);
		pa.text("2",300,660,145);
		pa.text("3",700,660,145);
		pa.popMatrix();
	}

}